package io.codegeneratetool.util;

import java.lang.reflect.Field;

import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

public class JPAUtil {

	public static String processJPAFieldName(Field field){ 
		
		
		return getManyToOneFieldName(field);
	}
	
	
	
	
	
	/** 获取 ManyToOne注解时的 映射字段名称
	 * 说明：
	 * @author jiaxf 创建时间2012-12-5下午2:29:52
	 * @param 
	 */
	public static String getManyToOneFieldName(Field field){
		ManyToOne annotation = field.getAnnotation(ManyToOne.class);
		String fieldname = field.getName();
		if(annotation!=null){  //如果有manytoone
			JoinColumn jc = field.getAnnotation(JoinColumn.class);
			if(jc!=null){
				fieldname = fieldname + "."+ jc.name();
			}
		}
		return fieldname;
	}
	
	/** 获取表名称
	 * @param t
	 * @return
	 */
	public static <T> String getTableName(T t){
		Table  table = t.getClass().getAnnotation(Table.class);
		if(table!=null){
			return table.name();
		}
		return "没有@Table注解";
	}
	
	
}
